Add custom JSON encoder/decoder option to Document constructor#397
Add custom JSON encoder/decoder option to Document constructor#397
Document constructor#397Conversation
| if self._document_id is not None: | ||
| self['_id'] = self._document_id | ||
| self.encoder = self._client.encoder | ||
| self.decoder = kwargs.get('decoder') or self._client.decoder |
There was a problem hiding this comment.
If the decoder is not configurable, can we remove kwargs.get('decoder')?
There was a problem hiding this comment.
It's only not configurable from the Cloudant client. We are allowing users to specify a custom decoder in the Document constructor.
A Client level decoder will be added in a future PR. It requires a little more work to ensure the decoder is passed around correctly.
ricellis
left a comment
There was a problem hiding this comment.
Looks good just a couple of suggested improvements.
| doc2.fetch() | ||
|
|
||
| self.assertEquals(doc2['name'], 'julia') | ||
| self.assertTrue(isinstance(doc2['dt'], datetime)) |
There was a problem hiding this comment.
Can't this and the following assertion be replaced by:
self.assertEquals(doc2['dt'], doc['dt'])?
Since doc and doc2 should both be using the object form and the datetime equality should work here.
| self.server_url = kwargs.get('url') | ||
| self._client_user_header = None | ||
| self.admin_party = admin_party | ||
| self.decoder = json.JSONDecoder # not currently configurable |
There was a problem hiding this comment.
Since this already caused some confusion, perhaps append at client scope or similar.
| if self._document_id is not None: | ||
| self['_id'] = self._document_id | ||
| self.encoder = self._client.encoder | ||
| self.decoder = kwargs.get('decoder') or self._client.decoder |
There was a problem hiding this comment.
Would be nice to document these parameters in the doc block.
1a8b4b6 to
7e262e2
Compare
7e262e2 to
8201741
Compare
Checklist
CHANGES.md|CHANGELOG.md) or test/build only changesDescription
Allow a user to specify a custom JSON encoder and decoder for a
Documenttype.Approach
Pass additional kwargs
encode&decodein via theDocumentconstructor.See test for example usage.
Schema & API Changes
Add additional kwargs to method call. No breaking API changes.
Security and Privacy
No change.
Testing
Adds
tests/unit/client_tests.py:ClientTests.test_constructor_with_url.Monitoring and Logging
No change.